#!/usr/bin/python3
#coding=utf8

import requests,time,execjs,json

def get_uc(current_time,page):
  #   js=execjs.compile(''' 
  #       const jsdom = require("jsdom");
		# const { JSDOM } = jsdom;
		# const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
		# window = dom.window;
		# document = window.document;
		# XMLHttpRequest = window.XMLHttpRequest;

  #       ''')
    
	# with open(r'jsdom.js','r',encoding='utf-8') as f:
	#     jsdom = f.read()
    # ct = execjs.compile(r'D:\TestCode\self_spider\test.js',cwd=r'D:\laragon\bin\nodejs\node-v12\node_modules')
    # 
    with open(r'D:\TestCode\self_spider\14.js', encoding='utf-8') as f:
        js = f.read()

    ct = execjs.compile(js,cwd=r'D:\laragon\bin\nodejs\node-v14\node_modules')
	# print(ct.call('Rohr_Opt.reload','1'))
    return ct.call('a',str(current_time)+'|'+str(page))
    # print(type(current_time))
    # print(ct)
    # print(ct.call('return_tokens',current_time))


# 参考的页面 有 
# https://stackoverflow.com/questions/64874484/js-referenceerror-fetch-is-not-defined
# https://cloud.tencent.com/developer/article/1647615
# https://www.cnblogs.com/songzhixue/p/11242221.html


# current_time = str(1629300036)
# print(current_time)
# print(get_safe(current_time))
if __name__ == '__main__':
  url = 'https://www.python-spider.com/api/challenge14'
  # current_time = '1631098468'
  # print(str(int(time.time())))
  # print(get_uc(current_time,3))
  
  try:
    totalnumber = int(0)
    for j in range(1,101):
      
        current_time = str(int(time.time()))
        headers = {
            # 'timestamp':timestamp,
            # 'safe':get_safe(timestamp)
        }
        param = {
            'page':j,
            'uc':get_uc(current_time,j),
        }
        res =  requests.request('post',url,headers=headers,data=param,timeout=20)
        
        jsonstrdata = json.loads(res.content)
        
        if 'data' in jsonstrdata:
            print(j,jsonstrdata['data'])
            for data_item in jsonstrdata['data']:
                if type(data_item['value']) == str:
                    totalnumber=totalnumber+int(data_item['value'].strip())
                else:
                    # print(data_item['value'])
                    pass
        else:
            pass
            # print(j);
            # print(jsonstrdata)
    print('---------------------------------------------------总的------------------------------------')
    print(totalnumber)
    
  except Exception as e:
    print('errors')
    print(e)
    print('errors')